<?php
    class UserBridge extends AppModel
    {
        var $name = "UserBridge";
        var $useTable = "user_bridge";
        function isRegistered($remoteId)
        {
            $userId = $this->field("user_id",array("remote_id"=>$remoteId));
            $registered = false;
            if($userId)
            {
                $registered = $this->getUserModel()->hasAny(array("id"=>$userId));
            }
            return $registered;
        }
        //@todo implemenent custom fields
        function register($data)
        {
            $requiredFields = array("email","username");
            $remoteId = $data['id'];
            unset($data['id']);
            if(!$remoteId)
            {
                return array("id" => "Please provide valid user id");
            }
            foreach($requiredFields as $requiredField)
            {
                if(!isset($data[$requiredField]))
                {
                    $data[$requiredField] = "";
                }
            }
            $data['confirm_password'] =  $this->getPassword($remoteId);
            $data['password'] =  Security::hash( $data['confirm_password'],null,true);
            $data['email_verified'] = 1;
            if(isset($data['registrationDate']))
            {
                $data['created'] = date("Y-m-d H:i:s",strtotime($data['registrationDate']));
            }
            $UserModel = $this->getUserModel();
            $UserModel->create();
            $saved = $UserModel->save(array($UserModel->alias => $data));
            if($saved)
            {
                $this->deleteAll(array("OR"=>array("user_id"=>$UserModel->id,"remote_id"=>$remoteId)));
                $this->create();
                $this->save(array($this->alias => array("remote_id"=>$remoteId,"user_id"=>$UserModel->id)));
                return true;
            }else
            {
                return $UserModel->validationErrors;
            }
        }
        
        private function getPassword($remoteId)
        {
            return "userpassre_".$remoteId;
        }
        /**
         * @return User 
         */
        function getUserModel()
        {
           return cri("Users.User");
        }
        
        function getLoginToken($remoteId)
        {
            if(!$remoteId)
            {
                return;
            }
            $info = $this->findByRemoteId($remoteId);
            if(!$info)
            {
                return;
            }
            $info = $info [$this->alias];
            $maxTime = 24*3600*5;
            $accessToken = $info['access_token'];
            if(!$info['access_token'] || strtotime($info['last_token_date']) < (time() - $maxTime))
            {
                $accessToken = md5(time()."_".$remoteId."_".Utility::generateRandomCode(3));
                $this->id = $info['id'];
                $this->saveField("access_token",$accessToken);
                $this->saveField("last_token_date",date("Y-m-d"));
            }
            return $accessToken;
        }
        
        function getUserFromToken($token)
        {
            $userId = $this->field("user_id",array("access_token"=>$token,"DATE(last_token_date) > DATE_SUB(CURDATE(),INTERVAL 10 DAY)"));
            return $userId;
        }
    }
?>